﻿using Module_Debug.Hardware;
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class RM690C0 : Form
    {
        #region 参数定义
        /// <summary>
        /// </summary>
        frmMain frmTop = null;
        CaSDK CaSDKObj = new CaSDK();//410
        CRC16 CRCData = new CRC16();//CRC
        public static bool firstGammaFlag = true;   //初次打开子窗体标志位
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private List<byte> buffer = new List<byte>(4096);//默认分配1/16页内存，并始终限制不允许超过
        private byte[] Rec_tmp = new byte[1];
        private int Multi_gamma_txt_now_index = 0;//当前调试gamma序号（组）
        private int Multi_gamma_txt_next_index;
        private int Multi_gamma_txt_line_count = 0;
        private int Multi_gamma_group_index = 0;
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        private int Total_points;//当前组gamma的绑点数
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double auto_lum;  //  存放获取到的产品亮度值
        private double auto_x;
        private double auto_y;        //  存放获取到的产品亮度 色坐标 x,y
        private double R_x = 0.679;//
        private double R_y = 0.321;
        private double G_x = 0.239;
        private double G_y = 0.717;
        private double B_x = 0.138;
        private double B_y = 0.0479;
        private double D_x, D_y;
        double Lr_d, Lg_d, Lb_d;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        byte rstep = 16; byte gstep = 16; byte bstep = 16;
        bool STOPAutoGammaFlag = true;     //AutoGamma 停止标志位，解决定时器禁止后AutoGamma无法停止的问题
        uint GammaValueMax = 0x3FF - 1;//  需要修改
        private int index = 1;  //AutoGamma 调整顺序
        bool JumpGammaAdjustFlag = false;  //跳过某个绑点标志位，通常用于末尾低灰阶
        private double lum_off, x_off, y_off;
        bool ReceivOKFlag = false;
        UInt32 timeout = 0;
        UInt32 Sum_timeout = 0;
        UInt32 single_timeout = 0;
        private int[] Point_RM690C0 = new int[255];//绑点灰阶，从低到高 需要修改
        byte GPN = 0xff; //保存Gamma绑点数量
        bool GammatuningOKFlag = false;
        private uint[,] GammaValue = new uint[256, 3];//保存当前Gammavalue
        bool lastdirupR = true; bool lastdirupG = true; bool lastdirupB = true; // Gamma 调整方向
        private byte[] Send_buf = new byte[1];
        int[] Receive_gamma_R = new int[27];
        int[] Receive_gamma_G = new int[27];
        int[] Receive_gamma_B = new int[27];
        bool load_pic_gamma = false;
        bool gamma_write_mode = true;
        bool load_bist_pattern = false;
		bool W_Flag = true;
        string[] num_endlogic = new string[15];
        string Gamma_loading_path = null;
        List<string> LCDCfg_PathList = new List<string>(12);
        int bist_mode = 50;
        string bist_code = null;

        private string[] gamma_r = new string[54]
           {
                  "0x00","0x01","0x02","0x03","0x04","0x05","0x06","0x07","0x08","0x09","0x0A","0x0B","0x0C","0x0D","0x0E","0x0F","0x10","0x11",
            "0x12","0x13","0x14","0x15","0x16","0x17","0x18","0x19","0x1A","0x1B","0x1C","0x1D","0x1E","0x1F","0x20","0x21","0x22","0x23",
            "0x24","0x25","0x26","0x27","0x28","0x29","0x2A","0x2B","0x2D","0x2F","0x30","0x31","0x32","0x33","0x34","0x35","0x36","0x37"
           };
        private string[] gamma_g = new string[54]
        {
                "0x38","0x39","0x3A","0x3B","0x3D","0x3F","0x40","0x41","0x42","0x43","0x44","0x45","0x46","0x47","0x48","0x49","0x4A","0x4B",
            "0x4C","0x4D","0x4E","0x4F","0x50","0x52","0x53","0x54","0x55","0x56","0x57","0x58","0x59","0x5A","0x5B","0x5C","0x5D","0x5E",
            "0x5F","0x60","0x61","0x62","0x63","0x64","0x65","0x66","0x67","0x68","0x69","0x6A","0x6B","0x6C","0x6D","0x6E","0x6F","0x70"
        };
        private string[] gamma_b = new string[54]
        {
                "0x71","0x72","0x73","0x74","0x75","0x76","0x77","0x78","0x79","0x7A","0x7B","0x7C","0x7D","0x7E","0x7F","0x80","0x81","0x82",
            "0x83","0x84","0x85","0x86","0x87","0x88","0x89","0x8A","0x8B","0x8C","0x8D","0x8E","0x8F","0x90","0x91","0x92","0x93","0x94",
            "0x95","0x96","0x97","0x98","0x99","0x9A","0x9B","0x9C","0x9D","0x9E","0x9F","0xA0","0xA2","0xA3","0xA4","0xA5","0xA6","0xA7"
        };
        #endregion
        public RM690C0(frmMain Frm)
        {
            frmTop = Frm;
            InitializeComponent();
            frmTop.Child_window_hide_already = true;
            frmTop.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体  
        }

        private void RM690C0_Gamma_write_Click(object sender, EventArgs e)//写入按钮
        {
            RM690C0_RegWR_Click(GammaPageSelect.Text);
            btnGammaValueUpdate_Click(null, null);
            //CheckandCorrectGammaValue();   //检查Gamma 是否存在高低灰阶数值错乱 ,并刷新表格数据
            if (gamma_write_mode == true)
            {
                for (byte i = 0; i < GPN; i++)//Point_RM690C0.Length; i++)
                {
                    GammaValueSet((byte)(i + 1)); //GammaValue 转换成特定格式后再通过此函数写入DDIC
                }
            }
            else
            {
                GammaValueSet((Byte)(GPN - 1)); //GammaValue 转换成特定格式后再通过此函数写入DDIC
            }

        }


        #region 脚本内容处理
        private void btn_flexible_gamma_Click(object sender, EventArgs e)//启动按钮
        {
            Sum_timeout = 0;           //gamma调试累计次数
            single_timeout = 0;          // 单绑点次数
            showmsg("开始调节gamma：", 2);
            Multi_gamma_txt_now_index = 0;
            Multi_gamma_txt_line_count = this.RM690C0_Gamma_SetBox.Lines.GetUpperBound(0);
            Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
            Application.DoEvents();
        }
        private void Multi_flexible_gammas(int txt_index, out int list_view_index)//单组gamma脚本的解析及执行
        {
            index = (int)num_index_start.Value + 1;  //Gamma绑点序号，第一个绑点通常对应255灰阶
            JumpGammaAdjustFlag = false;
            flag1 = flag2 = flag3 = 1;
            int flag = 1;


            string strLine = Convert.ToString(RM690C0_Gamma_SetBox.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组
            string perline = string.Empty;

            list_view_index = 0;

            if (frmMain.frmain.btnCal_manual.Tag.ToString() != "0calDone")
            {
                frmMain.frmain.btnCal_manual.Enabled = true;
                MessageBox.Show("需要提前连接410并校准~", "提示：");
                return;
            }

            comport_check();
            if (!frmMain.frmain.comPort.IsOpen)
            {
                showmsg("串口断开，请检查;", 3);
                return;
            }
            if (RM690C0_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("脚本内容为空!");
                return;
            }
            else  //标记关键事件
            {
                try
                {
                    arr = strLine.Split('\n'); //以逗号分割，添加到数
                    for (int i = txt_index; i < arr.Length;)  //获取的行数
                    {
                        perline = arr[i];
                        perline = perline.Replace(" ", "");
                        perline = perline.Replace(";", "");
                        perline = perline.Replace("(", "");
                        perline = perline.Replace(")", "");
                        perline = perline.Replace("\r", "");
                        perline = perline.Replace("\n", "");
                        perline = perline.ToUpper();

                        if (perline.Contains("//"))//允许脚本中存在注释，按照//符号以后的部分全部丢弃
                        {
                            int zhushi = perline.IndexOf("//");
                            if (zhushi == 0)
                            {
                                i = i + 1;
                                continue;
                            }
                            perline = perline.Substring(0, zhushi);
                        }

                        if (perline.Contains("REGW"))//脚本中一行含有 REGW时，触发写code模式
                        {
                            perline = perline.Replace("REGW", "");
                            RM690C0_RegWR_Click(perline);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("LOAD_BIST"))
                        {
                            perline = perline.Replace("LOAD_BIST", "");
                            perline = perline.Replace("_", "");
                            bist_code = perline.ToUpper();
                            i = i + 1;
                        }
                        else if (perline.Contains("SAVE"))////脚本中一行含有 SAVE时，触发自动保存gamma
                        {
                            btnSaveGamma_Click(btnSaveGamma, null);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("MEASURE_BRIGHTNESS"))////触发测量设定灰阶亮度功能
                        {
                            perline = perline.Replace("MEASURE_BRIGHTNESS", "");
                            Measure_brightness(perline);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
						else if (perline.Contains("MEASURE_RGB"))
						{
                            perline = perline.Replace("MEASURE_RGB", "");
                            Measure_RGB(perline);
                            Thread.Sleep(50);
                            i = i + 1;
						}
                        else if (perline.Contains("OLED_RESET"))////触发一键RESET模组
                        {
                            perline = perline.Replace("OLED_RESET", "");
                            showmsg("开始Reset:", 2);
                            frmTop.btnRun_Click(null, null);
                            Thread.Sleep(200);
                            showmsg("Reset完成:", 2);
                            i = i + 1;
                        }
                        else if (perline.Contains("GAMMA_PAGE"))//触发切换page，写code，并且将切page code保存在textbox中，在手动写gamma时会用到
                        {
                            perline = perline.Replace("GAMMA_PAGE", "");
                            perline = perline.Replace("_", "");
                            GammaPageSelect.Text = perline;
                            RM690C0_RegWR_Click(perline);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("LOGIC_GAMMA"))//指向主界面logic图调gamma
                        {
                            perline = perline.Replace("LOGIC_GAMMA", "");
                            perline = perline.Replace("_", "");
                            String[] perline_A = perline.Split(',');
                            for (int logi = 0; logi < perline_A.Length; logi++)
                            {
                                perline_A[logi] = perline_A[logi].Replace(":", "");
                                if (perline_A[logi].Contains("图形样式"))
                                {
                                    perline_A[logi] = perline_A[logi].Replace("图形样式", "");
                                    switch (perline_A[logi])
                                    {
                                        case "0":
                                            {
                                                frmTop.CBX_HFZ_LOGIC.SelectedIndex = 0;
                                            }
                                            break;
                                        case "1":
                                            {
                                                frmTop.CBX_HFZ_LOGIC.SelectedIndex = 1;
                                            }
                                            break;
                                        case "2":
                                            {
                                                frmTop.CBX_HFZ_LOGIC.SelectedIndex = 2;
                                            }
                                            break;
                                        case "3":
                                            {
                                                frmTop.CBX_HFZ_LOGIC.SelectedIndex = 3;
                                            }
                                            break;
                                        case "4":
                                            {
                                                frmTop.CBX_HFZ_LOGIC.SelectedIndex = 4;
                                            }
                                            break;
                                        default: break;
                                    }
                                }
                                if (perline_A[logi].Contains("X0"))
                                {
                                    perline_A[logi] = perline_A[logi].Replace("X0", "");
                                    frmTop.HFZ_X0.Value = Convert.ToInt32(perline_A[logi]);
                                }
                                if (perline_A[logi].Contains("X1"))
                                {
                                    perline_A[logi] = perline_A[logi].Replace("X1", "");
                                    frmTop.HFZ_X1.Value = Convert.ToInt32(perline_A[logi]);
                                }
                                if (perline_A[logi].Contains("Y0"))
                                {
                                    perline_A[logi] = perline_A[logi].Replace("Y0", "");
                                    frmTop.HFZ_Y0.Value = Convert.ToInt32(perline_A[logi]);
                                }
                                if (perline_A[logi].Contains("Y1"))
                                {
                                    perline_A[logi] = perline_A[logi].Replace("Y0", "");
                                    frmTop.HFZ_Y1.Value = Convert.ToInt32(perline_A[logi]);
                                }
                                if (perline_A[logi].Contains("R"))
                                {
                                    perline_A[logi] = perline_A[logi].Replace("R", "");
                                    frmTop.HFZ_R.Value = Convert.ToInt32(perline_A[logi]);
                                }
                            }
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("LOAD_BMP_GAMMA"))//用于load bmp图调gamma，指令格式：__LOAD_BMP_GAMMA(路径)；路径是文件夹路径，图片命名方式是：_255_.bmp 中间255是灰阶
                        {
                            perline = perline.Replace("__LOAD_BMP_GAMMA", "");
                            if (!Directory.Exists(perline))
                            {
                                showmsg("load bmp指向的路径不存在", 3);
                                return;
                            }
                            Gamma_loading_path = perline;
                            i = i + 1;
                        }
                        else if (perline.Contains("GAMMA_SET"))
                        {
                            frmTop.cbxlogic_go.Checked = false;//逻辑图开关默认关,以gamma_set为节点去检测是否是逻辑图或载图调gamma，是为了兼容在一组gamma调节后，使用Measure_brightness语句时，测量的画面不是调节的画面。
                            load_pic_gamma = false;//载图调gamma默认关
                            load_bist_pattern = false;


                            for (int i525 = txt_index; i525 < i; i525++)
                            {
                                if (arr[i525].Contains("LOAD_BMP_GAMMA"))
                                {
                                    load_pic_gamma = true;
                                }
                                if (arr[i525].Contains("LOGIC_GAMMA"))
                                {
                                    frmTop.cbxlogic_go.Checked = true;
                                }
                                if (arr[i525].Contains("LOAD_BIST"))
                                {
                                    if (arr[i525].Replace("LOAD_BIST", "").Length > 5)//根据loadbist指令长度判断功能，长度大于5时是固定格式，可以在脚本中编辑code，反之需要在软件中适配bistcode
                                    {
                                        load_bist_pattern = true;
                                    }
                                    else
                                    {
                                        frmTop.chkBISTmode.Checked = true;
                                    }
                                }
                            }

                            perline = perline.Replace("GAMMA_SET", "");
                            perline = perline.Replace("_", "");
                            //System.Diagnostics.Debug.Print(Multi_gamma_group_index.ToString());             // 华丽的分割线----------------------
                            GammaBandSelect.Text = perline;


                            ArrayList list = new ArrayList();
                            try
                            {
                                btnSaveGamma_Click(btnLoadGamma, null);
                                Application.DoEvents();
                            }
                            catch {; }

                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            GPN = (Byte)Diven_Offset.Count;
                            if (GPN != Point_RM690C0.Length)
                            {
                                showmsg("Gamma" + GammaBandSelect.Text + "脚本行数错误，只有" + GPN.ToString() + "行\r\n" + "应有" + Point_RM690C0.Length.ToString() + "行", 3);
                                return;
                            }

                            num_endlogic = tbx_end_logic.Text.Split(',');
                            if ((num_endlogic.Length != (num_index_end.Value - 1) * 3) && (num_endlogic.Length >1))
                            {
                                showmsg("结束赋值绑点数逻辑参数个数错误，应该有" + ((num_index_end.Value - 1) * 3).ToString() + "个", 3);
                                return;
                            }

                            try
                            {
                                RM690C0_Gamma_write_Click(null, null);
                                showmsg("写入默认值完成", 2);
                                Application.DoEvents();
                            }
                            catch {; }
                            switch (flag)
                            {
                                case 1://寄存器值与亮度正比
                                    {
                                        //lum_off = Convert.ToDouble(txtVT_loff.Text);
                                        x_off = Convert.ToDouble(txtVT_xoff.Text);   //获取色坐标允差范围
                                        y_off = Convert.ToDouble(txtVT_yoff.Text);
                                        set_pattern(Point_RM690C0[GPN - index]);

                                        Band_index.Text = "绑点:" + Point_RM690C0[GPN - index].ToString();
                                        Msr_CA310(); //修复讨厌的灰阶跳变时CA410数据异常的问题
                                        ReceivOKFlag = false;
                                        timeout = 0;
                                    }
                                    break;
                                case 2:  //寄存器值与亮度反比
                                    break;
                                default:
                                    break;
                            }
                            list_view_index = i;
                            STOPAutoGammaFlag = false;
                            GammaTuning();
                            break;
                        }
                        else
                        {
                            i = i + 1;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Gamma调节过程出错，请重试！");
                }

            }
        }
        private void RM690C0_RegWR_Click(string w_reg)//利用主界面黑窗写入code，写入包头按照主界面黑窗设定
        {
            string black_win = frmTop.Generate_textBox.Text;
            frmTop.Generate_textBox.Text = "IC WRITE\r\n" + w_reg;
            frmTop.Generate_File_Click(frmTop.Generate_File_Run, null);
            frmTop.Generate_textBox.Text = black_win;
        }
        private void Measure_brightness(string gray)//亮度测量语句的执行
        {
            string[] arr = gray.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                set_pattern(Convert.ToInt32(arr[i]));
                Thread.Sleep(1000);
                Msr_CA310();
                showmsg("W" + arr[i].ToString() + "的亮度是" + auto_lum.ToString("f4"), 2);
                showmsg("W" + arr[i].ToString() + "的x色坐标是" + auto_x.ToString("f4"), 2);
                showmsg("W" + arr[i].ToString() + "的y色坐标是" + auto_y.ToString("f4") + "\r\n", 2);
            }

        }
        private void Measure(string gray)//亮度测量语句的执行
        {
            string[] arr = gray.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
               // set_pattern(Convert.ToInt32(arr[i]));
                Thread.Sleep(1000);
                Msr_CA310();
                showmsg("W" + arr[i].ToString() + "的亮度是" + auto_lum.ToString("f4"), 2);
                showmsg("W" + arr[i].ToString() + "的x色坐标是" + auto_x.ToString("f4"), 2);
                showmsg("W" + arr[i].ToString() + "的y色坐标是" + auto_y.ToString("f4") + "\r\n", 2);
            }

        }
        private void Measure_RGB(string gray)//亮度测量语句的执行
        {
            string[] arr = gray.Split(',');
			if(frmMain.frmain.checkBox1.Checked == true)
			{
				W_Flag = true;
				frmMain.frmain.checkBox1.Checked = false;//
			}
            for (int i = 0; i < arr.Length-1; )
            {
                set_RGB(Convert.ToInt32(arr[i]),arr[i+1]);
                Thread.Sleep(1000);
                Msr_CA310();
                showmsg(arr[i+1].ToString() + arr[i].ToString() + "的亮度是" + auto_lum.ToString("f4"), 2);
                showmsg(arr[i+1].ToString() + arr[i].ToString() + "的x色坐标是" + auto_x.ToString("f4"), 2);
                showmsg(arr[i+1].ToString() + arr[i].ToString() + "的y色坐标是" + auto_y.ToString("f4") + "\r\n", 2);
				i=i+2;
            }
			if(W_Flag == true)
			{
				frmMain.frmain.checkBox1.Checked = true;//
			}
        }
		
        private void Text_Processing(string[] txt_path)//从gammaset（）开始至gammaturn结束
        {
            string[] arr = txt_path;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                            D_lum = new double[Diven_Gamma[0].Points_num];
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            if (arr1.Length > 4)//特定格式，在一个灰阶的spec中包含了亮度目标，2023年5月嘉辉提出的需求，所有绑点自定义亮度
                            {
                                D_lum[Diven_Gamma[0].Points_num - i] = Convert.ToDouble(arr1[3]);
                                Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[4]));
                                Diven_Offset.Add(Offset);
                            }
                            else
                            {
                                //将Offset对象放入集合
                                D_lum[Diven_Gamma[0].Points_num - i] = (Math.Pow((Convert.ToInt32(arr1[0]) / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                                Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                                Diven_Offset.Add(Offset);
                            }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("解析脚本错误，请检查脚本内容");
                }
            }
            try  //获取调整spec
            {
                numVT_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtVT_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtVT_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                Point_RM690C0 = new int[Total_points];

                txtVT_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtVT_x.Text = Diven_Gamma[0].X.ToString();
                txtVT_y.Text = Diven_Gamma[0].Y.ToString();

                D_x = Convert.ToDouble(txtVT_x.Text);  //获取调节目标色坐标 x
                D_y = Convert.ToDouble(txtVT_y.Text); ////获取调节目标色坐标 y
                Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
                Lb_d = 1.0;
                Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lums[Total_points - i - 1, 0] = D_lum[Total_points - i - 1] * Lr_d / (Lr_d + Lg_d + Lb_d);
                    D_lums[Total_points - i - 1, 1] = D_lum[Total_points - i - 1] * Lg_d / (Lr_d + Lg_d + Lb_d);
                    D_lums[Total_points - i - 1, 2] = D_lum[Total_points - i - 1] * Lb_d / (Lr_d + Lg_d + Lb_d);
                    Point_RM690C0[Total_points - i - 1] = Diven_Offset[i].NO;
                }

            }
            catch
            {
                MessageBox.Show("解析脚本错误，请检查脚本内容");
            }
        }
        private void btnVTStop_Click(object sender, EventArgs e)//停止按钮
        {
            STOPAutoGammaFlag = true;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            frmMain.frmain.textBox1.ForeColor = Color.Black;
        }
        #endregion

        private void button_Test_Click(object sender, EventArgs e)
        {
            if(STOPAutoGammaFlag == false)
				return;

			string strLine = Convert.ToString(RM690C0_Gamma_SetBox.Text);
			string[] arr = strLine.Split(','); //以逗号分割，添加到数组
			string perline = string.Empty;
			
			int list_view_index = 0;
			//int 

			if (RM690C0_Gamma_SetBox.Text == string.Empty)
			{
				MessageBox.Show("Empty cmd");
				return;
			}
			else
			{
                try
                {
                    arr = strLine.Split('\n'); //以逗号分割，添加到数
                    for (int i = 0; i < arr.Length;)  //获取的行数
                	{
                        perline = arr[i];
                        perline = perline.Replace(" ", "");
                        perline = perline.Replace(";", "");
                        perline = perline.Replace("(", "");
                        perline = perline.Replace(")", "");
                        perline = perline.Replace("\r", "");
                        perline = perline.Replace("\n", "");
                        perline = perline.ToUpper();

                        if (perline.Contains("REGW"))//脚本中一行含有 REGW时，触发写code模式
                        {
                            perline = perline.Replace("REGW", "");
                            RM690C0_RegWR_Click(perline);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
						else if(perline.Contains("FOR"))//FOR(58,1A,42,2,2000,255)
						{
                            perline = perline.Replace("FOR", "");
                            perline = perline.Replace("(", "");
                            perline = perline.Replace(")", "");
                            string[] arr0 = perline.Split(',');
							int loopStart = Convert.ToInt32(arr0[1],16);
							int loopEnd   = Convert.ToInt32(arr0[2],16);
							for(int loop = loopStart; loop < loopEnd+1; )
							{
								perline = arr0[0] + "," + Convert.ToString(loop,16);
								showmsg("RegData is ->" + Convert.ToString(loop,16), 2);
								RM690C0_RegWR_Click(perline);
								Thread.Sleep(Convert.ToInt32(arr0[4],16));
								Measure_brightness(arr0[5]);
								Thread.Sleep(50);
								loop = loop + Convert.ToInt32(arr0[3],16);
							}
                            i = i + 1;
							//break;
						}
                        else if (perline.Contains("MEASURE_BRIGHTNESS"))////触发测量设定灰阶亮度功能
                        {
                            perline = perline.Replace("MEASURE_BRIGHTNESS", "");
                            Measure_brightness(perline);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
						else if (perline.Contains("MEASURE_RGB"))
						{
                            perline = perline.Replace("MEASURE_RGB", "");
                            Measure_RGB(perline);
                            Thread.Sleep(50);
                            i = i + 1;
						}
						else if (perline.Contains("MEASURE"))
						{
                            perline = perline.Replace("MEASURE", "");
                            Measure_RGB(perline);
                            Thread.Sleep(50);
                            i = i + 1;
						}
                        else
                        {
                            i = i + 1;
                        }
					}
                }
                catch
                {
                    MessageBox.Show("Error CMD");
                }

            }
			
        }

        private void Load_gamma_csv(string bandNum)
        {
            int Line_Count = 0;
            string strtmp = "";

            string fname = Application.StartupPath + @"\l_gamma\" + cbx_IC_index.SelectedItem.ToString() + "band" + bandNum + ".csv";
            if (!File.Exists(fname))
            {
                showmsg(fname + "文件不存在 ", 3);
                return;
            }
            this.GammaReg_dataGridView.Rows.Clear();
            FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string strLine = streamReader.ReadLine();
            while (!string.IsNullOrEmpty(strLine))
            {
                strtmp = strtmp + strLine + ",";
                strLine = streamReader.ReadLine(); //循环读取下一行
                Line_Count++;
            }
            fs.Close(); //关闭流
            streamReader.Close();
            GPN = (byte)(Line_Count - 1);//用于未启动调gamma过的情况下，直接loadgamma 写入时，需要将GPN先刷新出来，避免写入时各项参数没有初始化导致的报错
            string[] arrtmp = strtmp.Split(new Char[] { ',' });
            for (int j = 1; j <= Line_Count - 1; j++)
            {
                DataGridViewRow Row1 = new DataGridViewRow();
                GammaReg_dataGridView.Rows.Add(Row1);
            }
            try
            {
                //showmsg("Gamma数据导入路径: " + fname, 2);
                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                {
                    GammaReg_dataGridView.Rows[i].Cells[0].Value = arrtmp[6 * (i + 1) + 0];
                    GammaReg_dataGridView.Rows[i].Cells[1].Value = arrtmp[6 * (i + 1) + 1];
                    GammaReg_dataGridView.Rows[i].Cells[2].Value = arrtmp[6 * (i + 1) + 2];
                    GammaReg_dataGridView.Rows[i].Cells[3].Value = arrtmp[6 * (i + 1) + 3];
                    GammaReg_dataGridView.Rows[i].Cells[4].Value = arrtmp[6 * (i + 1) + 4];
                    GammaReg_dataGridView.Rows[i].Cells[5].Value = arrtmp[6 * (i + 1) + 5];
                }
                //showmsg("Gammaband " + GammaBandSelect.Text + "  导入OK！", 2);
            }
            catch
            {
                MessageBox.Show("Gamma数据导入时发生错误！", "提示：");
            }
        }

        private void Btn_GammaWD_Click(object sender, EventArgs e)
        {
            int addr = 0;

            string path = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\Gamma" + ".wd";
            /*if (!File.Exists(path))
			{
				FileInfo cymfile = new FileInfo(path);
				FileStream fs = cymfile.Create();
				fs.Close();
			}*/
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            if (cbx_IC_index.SelectedIndex == 18)//WD6211
            {
                StreamWriter sw = File.AppendText(path);

                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    addr = 33024 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString(((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0xF0) >> 4), 16));

                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString(((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0x0F) << 4), 16));
                }
                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                {
                    this.GammaReg_dataGridView.Rows[i].Cells[0].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[1].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00) >> 8;
                    this.GammaReg_dataGridView.Rows[i].Cells[2].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[3].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFF00) >> 8;
                    this.GammaReg_dataGridView.Rows[i].Cells[4].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[5].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFF00) >> 8;

                }
                //R			
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 33088 + 2 * loop;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)), 16));

                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value)), 16));
                    }
                    else
                    {
                        addr = 33090 + 2 * (loop - 1);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                    }
                }

                //G
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 33152 + 2 * loop;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)), 16));

                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value)), 16));
                    }
                    else
                    {
                        addr = 33154 + 2 * (loop - 1);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                    }
                }

                //B
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 33216 + 2 * loop;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)), 16));

                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)), 16));
                    }
                    else
                    {
                        addr = 33218 + 2 * (loop - 1);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                    }
                }

                sw.WriteLine("wr 0x1200 0x00");
                sw.WriteLine("wr 0x1201 0x00");
                sw.WriteLine("wr 0x1202 0x00");//("wr 0x1202 0x04") V06 start change to 00
                sw.WriteLine("wr 0x1203 0xbf");
                sw.WriteLine("wr 0x1204 0x03");
                sw.WriteLine("wr 0x1205 0x30");
                sw.WriteLine("wr 0x1220 0x01");
                sw.WriteLine("wr 0x1221 0x84");
                sw.WriteLine("wr 0x1222 0x42");
                sw.WriteLine("wr 0x1000 0x00");
                sw.WriteLine("wr 0x1010 0x01");

                sw.Flush();
                sw.Close();
            }
            else if (cbx_IC_index.SelectedIndex == 22)//SPI
            {
                string path_spi = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\Gamma_SPI" + ".txt";
                if (File.Exists(path_spi))
                {
                    File.Delete(path_spi);
                }
                StreamWriter sw2 = File.AppendText(path_spi);
                sw2.WriteLine("IC WRITE//WD6211+WD3100");
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x00 + 2 * loop)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0xF0) >> 4)) + ",00");
                    sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x00 + 2 * loop + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0x0F) << 4)) + ",00");
                }

                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop == 0)
                    {
                        sw2.WriteLine("FF,FF,F9,50,81,40,03,00");
                    }
                    else
                    {
                        sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x40 + 2 * loop)) + "," + string.Format("{0:X2}", (((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00) >> 8)) + ",00");
                    }
                    sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x40 + 2 * loop + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF)) + ",00");
                }

                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop == 0)
                    {
                        sw2.WriteLine("FF,FF,F9,50,81,80,03,00");
                    }
                    else
                    {
                        sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x80 + 2 * loop)) + "," + string.Format("{0:X2}", (((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00) >> 8)) + ",00");
                    }
                    sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x80 + 2 * loop + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF)) + ",00");
                }

                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop == 0)
                    {
                        sw2.WriteLine("FF,FF,F9,50,81,C0,03,00");
                    }
                    else
                    {
                        sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0xC0 + 2 * loop)) + "," + string.Format("{0:X2}", (((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00) >> 8)) + ",00");
                    }
                    sw2.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0xC0 + 2 * loop + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF)) + ",00");
                }

                sw2.Flush();
                sw2.Close();



                StreamWriter sw = File.AppendText(path);
                sw.WriteLine("//WD6211+WD3100");
                //ff,ff,f9,50,81,41,00,00
                //wr 0x8141 0x00
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    addr = 33024 + 2 * loop;
                    //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString(((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0xF0) >> 4), 16));
                    //string.Format("{0:X2}", (gdattmp >> 8))
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0xF0) >> 4)));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) & 0x0F) << 4)));
                }
                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                {
                    this.GammaReg_dataGridView.Rows[i].Cells[0].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[1].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00) >> 8;
                    this.GammaReg_dataGridView.Rows[i].Cells[2].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[3].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFF00) >> 8;
                    this.GammaReg_dataGridView.Rows[i].Cells[4].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[5].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFF00) >> 8;

                }
                //R 		
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 33088 + 2 * loop;
                        if (loop == 0)
                        {
                            sw.WriteLine("wr 0x8140 0x03");
                        }
                        else
                        {
                            //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)), 16));
                            sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value))));
                        }
                        addr = addr + 1;
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value)), 16));
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value))));
                    }
                    else
                    {
                        addr = 33090 + 2 * (loop - 1);
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value))));
                        addr = addr + 1;
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value))));
                    }
                }

                //G
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 33152 + 2 * loop;
                        if (loop == 0)
                        {
                            sw.WriteLine("wr 0x8180 0x03");
                        }
                        else
                        {
                            //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)), 16));
                            sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value))));
                        }
                        addr = addr + 1;
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value)), 16));
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value))));
                    }
                    else
                    {
                        addr = 33154 + 2 * (loop - 1);
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value))));
                        addr = addr + 1;
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value))));
                    }
                }

                //B
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 33216 + 2 * loop;
                        if (loop == 0)
                        {
                            sw.WriteLine("wr 0x81c0 0x03");
                        }
                        else
                        {
                            //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)), 16));
                            sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value))));
                        }
                        addr = addr + 1;
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)), 16));
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value))));
                    }
                    else
                    {
                        addr = 33218 + 2 * (loop - 1);
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value))));
                        addr = addr + 1;
                        //sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value))));
                    }
                }

                sw.WriteLine("wr 0x1200 0x00");
                sw.WriteLine("wr 0x1201 0x00");//0830 change to 1FF. 0902 change to 000 &0 level 0x300
                sw.WriteLine("wr 0x1202 0x00");//("wr 0x1202 0x04") V06 start change to 00
                sw.WriteLine("wr 0x1203 0xbf");
                sw.WriteLine("wr 0x1204 0x03");
                sw.WriteLine("wr 0x1205 0x30");
                sw.WriteLine("wr 0x1220 0x01");
                sw.WriteLine("wr 0x1221 0x84");
                sw.WriteLine("wr 0x1222 0x42");
                sw.WriteLine("wr 0x1000 0x00");
                sw.WriteLine("wr 0x1010 0x01");

                sw.Flush();
                sw.Close();

            }
            else if (cbx_IC_index.SelectedIndex == 23 || cbx_IC_index.SelectedIndex == 24)//WD6300_SPI & MIPI
            {
                string path_6300 = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\gammaband_cfg" + ".wd";
                if (File.Exists(path_6300))
                {
                    File.Delete(path_6300);
                }
                StreamWriter sw = File.AppendText(path_6300);

                //Gamma node
                sw.WriteLine("//Gamma node");
                for (int loop = 1; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0xF0;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0x0F;
                    }

                    addr = 0x5210 + 2 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 4));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow << 4));
                }

                //gamma multiply factor
                sw.WriteLine("//gamma multiply factor");
                for (int loop = 1; loop < 33; loop++)
                {

                    //厂商给的代码是读取寄存器的值，所以cur以及pre需要左移4位
                    int factor = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        if (loop == 1)
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur));
                        }
                        else
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            int pre = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop - 1].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur - pre));//factor计算公式
                        }
                    }

                    addr = 0x5250 + 3 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 16 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 8 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor & 0xff)));
                }

                if ((this.GammaReg_dataGridView.Rows.Count - 1) < 33)
                {

                    //dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 //gamma node num = 0x16
                    sw.WriteLine("wr 0x53b0 0x07");
                    sw.WriteLine("wr 0x53b1 0x07");
                    sw.WriteLine("wr 0x53b2 0x2b");
                    sw.WriteLine("wr 0x53b3 0xf2");
                    sw.WriteLine("wr 0x53b4 0x" + string.Format("{0:X2}", ((this.GammaReg_dataGridView.Rows.Count - 1) & 0xff)));
                }

                //write R curve (//0=wr R   1=wr G   2=wr B)
                //Red
                sw.WriteLine("//Red");
                //dcswr一次要传输16byte，所以0x51f0->0x51f6以及0x8100->0x8107可以不用写或者随便赋值。
                /*                sw.WriteLine("wr 0x51f0 0x00");
                                sw.WriteLine("wr 0x51f1 0x00");
                                sw.WriteLine("wr 0x51f2 0x00");
                                sw.WriteLine("wr 0x51f3 0x00");
                                sw.WriteLine("wr 0x51f4 0x00");
                                sw.WriteLine("wr 0x51f5 0x00");
                                sw.WriteLine("wr 0x51f6 0x80");*/
                sw.WriteLine("wr 0x51f7 0x00");//rif_bc_dbv_gamma_curve_color_sel: select R
                /*                sw.WriteLine("wr 0x8100 0x00");
                                sw.WriteLine("wr 0x8101 0x00");
                                sw.WriteLine("wr 0x8102 0x00");
                                sw.WriteLine("wr 0x8103 0x10");
                                sw.WriteLine("wr 0x8104 0x00");
                                sw.WriteLine("wr 0x8105 0x40");
                                sw.WriteLine("wr 0x8106 0x00");
                                sw.WriteLine("wr 0x8107 0x80");*/
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Green
                sw.WriteLine("//Green");
                sw.WriteLine("wr 0x51f7 0x01");//Green
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Blue
                sw.WriteLine("//Blue");
                sw.WriteLine("wr 0x51f7 0x02");//Blue
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.Flush();
                sw.Close();
            }
            else if (cbx_IC_index.SelectedIndex == 0x1A)//WD6300_MIPI_6.3灵动岛
            {
                string path_6300 = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\gammaband_cfg" + ".wd";
                if (File.Exists(path_6300))
                {
                    File.Delete(path_6300);
                }
                StreamWriter sw = File.AppendText(path_6300);

                //Gamma node
                sw.WriteLine("//Gamma node");
                for (int loop = 1; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0xF0;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0x0F;
                    }

                    addr = 0x5210 + 2 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 4));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow << 4));
                }

                //gamma multiply factor
                sw.WriteLine("//gamma multiply factor");
                for (int loop = 1; loop < 33; loop++)
                {
                    //厂商给的代码是读取寄存器的值，所以cur以及pre需要左移4位
                    int factor = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        if (loop == 1)
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur));
                        }
                        else
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            int pre = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop - 1].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur - pre));//factor计算公式
                        }
                    }

                    addr = 0x5250 + 3 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 16 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 8 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor & 0xff)));
                }

                //Gamma node num
                sw.WriteLine("//dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 gamma node num = 0x16");
                if ((this.GammaReg_dataGridView.Rows.Count - 1) < 33)
                {
                    //dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 //gamma node num = 0x16
                    sw.WriteLine("wr 0x53b0 0x07");
                    sw.WriteLine("wr 0x53b1 0x07");
                    sw.WriteLine("wr 0x53b2 0x2b");
                    sw.WriteLine("wr 0x53b3 0xf2");
                    sw.WriteLine("wr 0x53b4 0x" + string.Format("{0:X2}", ((this.GammaReg_dataGridView.Rows.Count - 1) & 0xff)));
                }

                //write R curve (//0=wr R   1=wr G   2=wr B)
                //Red
                sw.WriteLine("//Gamma value");
                //dcswr一次要传输16byte，所以0x51f0->0x51f6以及0x8100->0x8107可以不用写或者随便赋值。
                /*                sw.WriteLine("wr 0x51f0 0x00");
                                sw.WriteLine("wr 0x51f1 0x00");
                                sw.WriteLine("wr 0x51f2 0x00");
                                sw.WriteLine("wr 0x51f3 0x00");
                                sw.WriteLine("wr 0x51f4 0x00");
                                sw.WriteLine("wr 0x51f5 0x00");
                                sw.WriteLine("wr 0x51f6 0x80");*/
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select R");
                sw.WriteLine("wr 0x51f7 0x00");//rif_bc_dbv_gamma_curve_color_sel: select R
                /*                sw.WriteLine("wr 0x8100 0x00");
                                sw.WriteLine("wr 0x8101 0x00");
                                sw.WriteLine("wr 0x8102 0x00");
                                sw.WriteLine("wr 0x8103 0x10");
                                sw.WriteLine("wr 0x8104 0x00");
                                sw.WriteLine("wr 0x8105 0x40");
                                sw.WriteLine("wr 0x8106 0x00");
                                sw.WriteLine("wr 0x8107 0x80");*/
                sw.WriteLine("//Red1");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//Red2");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//Red3");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Green
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select G");
                sw.WriteLine("wr 0x51f7 0x01");
                sw.WriteLine("//Green1");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//Green2");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//Green3");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Blue
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select B");
                sw.WriteLine("wr 0x51f7 0x02");//Blue
                sw.WriteLine("//Blue1");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//Blue2");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//Blue3");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.Flush();
                sw.Close();
                showmsg("输出gammaband_cfg.wd文件到" + path_6300, 2);
            }
            else if (cbx_IC_index.SelectedIndex == 0x1B)//WD6300_MIPI_6.1灵动岛
            {
                string path_6300 = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\gammaband_cfg" + ".wd";
                if (File.Exists(path_6300))
                {
                    File.Delete(path_6300);
                }
                StreamWriter sw = File.AppendText(path_6300);

                //Gamma node
                sw.WriteLine("//Gamma node");
                for (int loop = 1; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0xF0;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0x0F;
                    }

                    addr = 0x5210 + 2 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 4));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow << 4));
                }

                //gamma multiply factor
                sw.WriteLine("//gamma multiply factor");
                for (int loop = 1; loop < 33; loop++)
                {
                    //厂商给的代码是读取寄存器的值，所以cur以及pre需要左移4位
                    int factor = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        if (loop == 1)
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur));
                        }
                        else
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            int pre = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop - 1].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur - pre));//factor计算公式
                        }
                    }

                    addr = 0x5250 + 3 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 16 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 8 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor & 0xff)));
                }

                //Gamma node num
                sw.WriteLine("//dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 gamma node num = 0x16");
                if ((this.GammaReg_dataGridView.Rows.Count - 1) < 33)
                {
                    //dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 //gamma node num = 0x16
                    sw.WriteLine("wr 0x53b0 0x07");
                    sw.WriteLine("wr 0x53b1 0x07");
                    sw.WriteLine("wr 0x53b2 0x2b");
                    sw.WriteLine("wr 0x53b3 0xf2");
                    sw.WriteLine("wr 0x53b4 0x" + string.Format("{0:X2}", ((this.GammaReg_dataGridView.Rows.Count - 1) & 0xff)));
                }

                //write R curve (//0=wr R   1=wr G   2=wr B)
                //Red
                sw.WriteLine("//Gamma value");
                //dcswr一次要传输16byte，所以0x51f0->0x51f6以及0x8100->0x8107可以不用写或者随便赋值。
                /*                sw.WriteLine("wr 0x51f0 0x00");
                                sw.WriteLine("wr 0x51f1 0x00");
                                sw.WriteLine("wr 0x51f2 0x00");
                                sw.WriteLine("wr 0x51f3 0x00");
                                sw.WriteLine("wr 0x51f4 0x00");
                                sw.WriteLine("wr 0x51f5 0x00");
                                sw.WriteLine("wr 0x51f6 0x80");*/
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select R");
                sw.WriteLine("wr 0x51f7 0x00");//rif_bc_dbv_gamma_curve_color_sel: select R
                /*                sw.WriteLine("wr 0x8100 0x00");
                                sw.WriteLine("wr 0x8101 0x00");
                                sw.WriteLine("wr 0x8102 0x00");
                                sw.WriteLine("wr 0x8103 0x10");
                                sw.WriteLine("wr 0x8104 0x00");
                                sw.WriteLine("wr 0x8105 0x40");
                                sw.WriteLine("wr 0x8106 0x00");
                                sw.WriteLine("wr 0x8107 0x80");*/
                sw.WriteLine("//R1 AOD band4");
                Load_gamma_csv("50");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                Load_gamma_csv("51");//read 1B.WD6300_6.1band51.csv
                sw.WriteLine("//R2 120Hz band5");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//R3 60Hz band6");
                Load_gamma_csv("50");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Green
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select G");
                sw.WriteLine("wr 0x51f7 0x01");
                sw.WriteLine("//G1 AOD band4");
                Load_gamma_csv("50");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//G2 120Hz band5");
                Load_gamma_csv("51");//read 1B.WD6300_6.1band51.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//G3 60Hz band6 ");
                Load_gamma_csv("50");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Blue
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select B");
                sw.WriteLine("wr 0x51f7 0x02");//Blue
                sw.WriteLine("//B1 AOD band4");
                Load_gamma_csv("50");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//B2 120Hz band5");
                Load_gamma_csv("51");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.WriteLine("//B3 60Hz band6");
                Load_gamma_csv("50");//read 1B.WD6300_6.1band50.csv
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }
                sw.Flush();
                sw.Close();
                showmsg("输出gammaband_cfg.wd文件到" + path_6300, 2);
            }
            else if (cbx_IC_index.SelectedIndex == 21)//WD6200
            {
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine("wr 0x1010 0x00");
                sw.WriteLine("wr 0x1011 0x00");
                sw.WriteLine("wr 0x1012 0x00");
                sw.WriteLine("wr 0x1013 0x16");
                sw.WriteLine("wr 0x1014 0x00");
                sw.WriteLine("wr 0x1015 0x9b");
                sw.WriteLine("wr 0x1016 0x00");
                sw.WriteLine("wr 0x1017 0xf3");
                sw.WriteLine("wr 0x1018 0x00");
                sw.WriteLine("wr 0x1019 0x5d");
                sw.WriteLine("wr 0x101a 0x00");
                sw.WriteLine("wr 0x101b 0x8d");
                sw.WriteLine("wr 0x101c 0x00");
                sw.WriteLine("wr 0x101d 0xcd");
                sw.WriteLine("wr 0x101e 0x01");
                sw.WriteLine("wr 0x101f 0x5d");
                sw.WriteLine("wr 0x1020 0x02");
                sw.WriteLine("wr 0x1021 0x5e");
                sw.WriteLine("wr 0x1022 0x03");
                sw.WriteLine("wr 0x1023 0x2f");
                sw.WriteLine("wr 0x1024 0x03");
                sw.WriteLine("wr 0x1025 0xff");
                sw.WriteLine("wr 0x1000 0xff");
                sw.WriteLine("wr 0x1001 0xf2");
                sw.WriteLine("wr 0x1002 0x42");
                sw.WriteLine("wr 0x44d0 0x01");
                sw.WriteLine("wr 0x44d1 0x8a");
                sw.WriteLine("wr 0x44d2 0x00");
                sw.WriteLine("wr 0x44d3 0x01");
                sw.WriteLine("wr 0x44d4 0x00");
                sw.WriteLine("wr 0x44d5 0x40");
                sw.WriteLine("wr 0x44d6 0x00");
                sw.WriteLine("wr 0x44d7 0x80");
                sw.WriteLine("wr 0x44d8 0x00");
                sw.WriteLine("wr 0x44d9 0xc0");
                sw.WriteLine("wr 0x44da 0x01");
                sw.WriteLine("wr 0x44db 0x00");
                sw.WriteLine("wr 0x44dc 0x01");
                sw.WriteLine("wr 0x44dd 0x70");
                sw.WriteLine("wr 0x44de 0x02");
                sw.WriteLine("wr 0x44df 0x20");
                sw.WriteLine("wr 0x44e0 0x02");
                sw.WriteLine("wr 0x44e1 0x60");
                sw.WriteLine("wr 0x44e2 0x03");
                sw.WriteLine("wr 0x44e3 0x20");
                sw.WriteLine("wr 0x44e4 0x03");
                sw.WriteLine("wr 0x44e5 0x80");
                sw.WriteLine("wr 0x44e6 0x04");
                sw.WriteLine("wr 0x44e7 0x40");
                sw.WriteLine("wr 0x44e8 0x05");
                sw.WriteLine("wr 0x44e9 0x60");
                sw.WriteLine("wr 0x44ea 0x06");
                sw.WriteLine("wr 0x44eb 0x60");
                sw.WriteLine("wr 0x44ec 0x06");
                sw.WriteLine("wr 0x44ed 0xf0");
                sw.WriteLine("wr 0x44ee 0x08");
                sw.WriteLine("wr 0x44ef 0x00");
                sw.WriteLine("wr 0x44f0 0x09");
                sw.WriteLine("wr 0x44f1 0xb0");
                sw.WriteLine("wr 0x44f2 0x0a");
                sw.WriteLine("wr 0x44f3 0x20");
                sw.WriteLine("wr 0x44f4 0x0a");
                sw.WriteLine("wr 0x44f5 0xe0");
                sw.WriteLine("wr 0x44f6 0x0b");
                sw.WriteLine("wr 0x44f7 0x70");
                sw.WriteLine("wr 0x44f8 0x0c");
                sw.WriteLine("wr 0x44f9 0x30");
                sw.WriteLine("wr 0x44fa 0x0c");
                sw.WriteLine("wr 0x44fb 0xa0");
                sw.WriteLine("wr 0x44fc 0x0c");
                sw.WriteLine("wr 0x44fd 0xe0");
                sw.WriteLine("wr 0x44fe 0x0d");
                sw.WriteLine("wr 0x44ff 0xb0");
                sw.WriteLine("wr 0x4500 0x0e");
                sw.WriteLine("wr 0x4501 0x20");
                sw.WriteLine("wr 0x4502 0x0e");
                sw.WriteLine("wr 0x4503 0x90");
                sw.WriteLine("wr 0x4504 0x0e");
                sw.WriteLine("wr 0x4505 0xc0");
                sw.WriteLine("wr 0x4506 0x0f");
                sw.WriteLine("wr 0x4507 0x20");
                sw.WriteLine("wr 0x4508 0x0f");
                sw.WriteLine("wr 0x4509 0x70");
                sw.WriteLine("wr 0x450a 0x0f");
                sw.WriteLine("wr 0x450b 0xa0");
                sw.WriteLine("wr 0x450c 0x0f");
                sw.WriteLine("wr 0x450d 0xd0");
                sw.WriteLine("wr 0x450e 0x0f");
                sw.WriteLine("wr 0x450f 0xf0");
                sw.WriteLine("wr 0x4510 0x01");
                sw.WriteLine("wr 0x4511 0x8a");
                sw.WriteLine("wr 0x4512 0x00");
                sw.WriteLine("wr 0x4513 0x01");
                sw.WriteLine("wr 0x4514 0x00");
                sw.WriteLine("wr 0x4515 0x40");
                sw.WriteLine("wr 0x4516 0x00");
                sw.WriteLine("wr 0x4517 0x80");
                sw.WriteLine("wr 0x4518 0x00");
                sw.WriteLine("wr 0x4519 0xc0");
                sw.WriteLine("wr 0x451a 0x01");
                sw.WriteLine("wr 0x451b 0x00");
                sw.WriteLine("wr 0x451c 0x01");
                sw.WriteLine("wr 0x451d 0x70");
                sw.WriteLine("wr 0x451e 0x02");
                sw.WriteLine("wr 0x451f 0x20");
                sw.WriteLine("wr 0x4520 0x02");
                sw.WriteLine("wr 0x4521 0x60");
                sw.WriteLine("wr 0x4522 0x03");
                sw.WriteLine("wr 0x4523 0x20");
                sw.WriteLine("wr 0x4524 0x03");
                sw.WriteLine("wr 0x4525 0x80");
                sw.WriteLine("wr 0x4526 0x04");
                sw.WriteLine("wr 0x4527 0x40");
                sw.WriteLine("wr 0x4528 0x05");
                sw.WriteLine("wr 0x4529 0x60");
                sw.WriteLine("wr 0x452a 0x06");
                sw.WriteLine("wr 0x452b 0x60");
                sw.WriteLine("wr 0x452c 0x06");
                sw.WriteLine("wr 0x452d 0xf0");
                sw.WriteLine("wr 0x452e 0x08");
                sw.WriteLine("wr 0x452f 0x00");
                sw.WriteLine("wr 0x4530 0x09");
                sw.WriteLine("wr 0x4531 0xb0");
                sw.WriteLine("wr 0x4532 0x0a");
                sw.WriteLine("wr 0x4533 0x20");
                sw.WriteLine("wr 0x4534 0x0a");
                sw.WriteLine("wr 0x4535 0xe0");
                sw.WriteLine("wr 0x4536 0x0b");
                sw.WriteLine("wr 0x4537 0x70");
                sw.WriteLine("wr 0x4538 0x0c");
                sw.WriteLine("wr 0x4539 0x30");
                sw.WriteLine("wr 0x453a 0x0c");
                sw.WriteLine("wr 0x453b 0xa0");
                sw.WriteLine("wr 0x453c 0x0c");
                sw.WriteLine("wr 0x453d 0xe0");
                sw.WriteLine("wr 0x453e 0x0d");
                sw.WriteLine("wr 0x453f 0xb0");
                sw.WriteLine("wr 0x4540 0x0e");
                sw.WriteLine("wr 0x4541 0x20");
                sw.WriteLine("wr 0x4542 0x0e");
                sw.WriteLine("wr 0x4543 0x90");
                sw.WriteLine("wr 0x4544 0x0e");
                sw.WriteLine("wr 0x4545 0xc0");
                sw.WriteLine("wr 0x4546 0x0f");
                sw.WriteLine("wr 0x4547 0x20");
                sw.WriteLine("wr 0x4548 0x0f");
                sw.WriteLine("wr 0x4549 0x70");
                sw.WriteLine("wr 0x454a 0x0f");
                sw.WriteLine("wr 0x454b 0xa0");
                sw.WriteLine("wr 0x454c 0x0f");
                sw.WriteLine("wr 0x454d 0xd0");
                sw.WriteLine("wr 0x454e 0x0f");
                sw.WriteLine("wr 0x454f 0xf0");
                sw.WriteLine("wr 0x4550 0x01");
                sw.WriteLine("wr 0x4551 0x8a");
                sw.WriteLine("wr 0x4552 0x00");
                sw.WriteLine("wr 0x4553 0x01");
                sw.WriteLine("wr 0x4554 0x00");
                sw.WriteLine("wr 0x4555 0x40");
                sw.WriteLine("wr 0x4556 0x00");
                sw.WriteLine("wr 0x4557 0x80");
                sw.WriteLine("wr 0x4558 0x00");
                sw.WriteLine("wr 0x4559 0xc0");
                sw.WriteLine("wr 0x455a 0x01");
                sw.WriteLine("wr 0x455b 0x00");
                sw.WriteLine("wr 0x455c 0x01");
                sw.WriteLine("wr 0x455d 0x70");
                sw.WriteLine("wr 0x455e 0x02");
                sw.WriteLine("wr 0x455f 0x20");
                sw.WriteLine("wr 0x4560 0x02");
                sw.WriteLine("wr 0x4561 0x60");
                sw.WriteLine("wr 0x4562 0x03");
                sw.WriteLine("wr 0x4563 0x20");
                sw.WriteLine("wr 0x4564 0x03");
                sw.WriteLine("wr 0x4565 0x80");
                sw.WriteLine("wr 0x4566 0x04");
                sw.WriteLine("wr 0x4567 0x40");
                sw.WriteLine("wr 0x4568 0x05");
                sw.WriteLine("wr 0x4569 0x60");
                sw.WriteLine("wr 0x456a 0x06");
                sw.WriteLine("wr 0x456b 0x60");
                sw.WriteLine("wr 0x456c 0x06");
                sw.WriteLine("wr 0x456d 0xf0");
                sw.WriteLine("wr 0x456e 0x08");
                sw.WriteLine("wr 0x456f 0x00");
                sw.WriteLine("wr 0x4570 0x09");
                sw.WriteLine("wr 0x4571 0xb0");
                sw.WriteLine("wr 0x4572 0x0a");
                sw.WriteLine("wr 0x4573 0x20");
                sw.WriteLine("wr 0x4574 0x0a");
                sw.WriteLine("wr 0x4575 0xe0");
                sw.WriteLine("wr 0x4576 0x0b");
                sw.WriteLine("wr 0x4577 0x70");
                sw.WriteLine("wr 0x4578 0x0c");
                sw.WriteLine("wr 0x4579 0x30");
                sw.WriteLine("wr 0x457a 0x0c");
                sw.WriteLine("wr 0x457b 0xa0");
                sw.WriteLine("wr 0x457c 0x0c");
                sw.WriteLine("wr 0x457d 0xe0");
                sw.WriteLine("wr 0x457e 0x0d");
                sw.WriteLine("wr 0x457f 0xb0");
                sw.WriteLine("wr 0x4580 0x0e");
                sw.WriteLine("wr 0x4581 0x20");
                sw.WriteLine("wr 0x4582 0x0e");
                sw.WriteLine("wr 0x4583 0x90");
                sw.WriteLine("wr 0x4584 0x0e");
                sw.WriteLine("wr 0x4585 0xc0");
                sw.WriteLine("wr 0x4586 0x0f");
                sw.WriteLine("wr 0x4587 0x20");
                sw.WriteLine("wr 0x4588 0x0f");
                sw.WriteLine("wr 0x4589 0x70");
                sw.WriteLine("wr 0x458a 0x0f");
                sw.WriteLine("wr 0x458b 0xa0");
                sw.WriteLine("wr 0x458c 0x0f");
                sw.WriteLine("wr 0x458d 0xd0");
                sw.WriteLine("wr 0x458e 0x0f");
                sw.WriteLine("wr 0x458f 0xf0");
                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                {
                    this.GammaReg_dataGridView.Rows[i].Cells[0].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[1].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00) >> 8;
                    this.GammaReg_dataGridView.Rows[i].Cells[2].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[3].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFF00) >> 8;
                    this.GammaReg_dataGridView.Rows[i].Cells[4].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFF;
                    this.GammaReg_dataGridView.Rows[i].Cells[5].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFF00) >> 8;

                }
                //R			
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 4352 + 2 * loop;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)), 16));

                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value)), 16));
                    }
                    else
                    {
                        addr = 4352 + 2 + 2 * (loop - 1);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                    }
                }

                //G
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 4352 + 64 + 2 * loop;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)), 16));

                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value)), 16));
                    }
                    else
                    {
                        addr = 4352 + 64 + 2 + 2 * (loop - 1);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                    }
                }

                //B
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    if (loop % 2 == 0)//ji
                    {
                        addr = 4352 + 128 + 2 * loop;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)), 16));

                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)), 16));
                    }
                    else
                    {
                        addr = 4352 + 128 + 2 + 2 * (loop - 1);
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)), 16));// +(Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF);
                        addr = addr + 1;
                        sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + Convert.ToString((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)), 16));//+((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFF00)>>8);
                    }
                }
                sw.WriteLine("wr 0x11c0 0x03");
                sw.Flush();
                sw.Close();

            }
            else if (cbx_IC_index.SelectedIndex == 0)//for RM6D030 USE  //50 60 30 52
            {
                string path_RM6D030 = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\RM6D030_1_Gamma_readback" + ".txt";
                if (File.Exists(path_RM6D030))
                {
                    File.Delete(path_RM6D030);
                }
                StreamWriter sw2 = File.AppendText(path_RM6D030);

                for (int index = 0; index < 8; index++)//50-60-30-52-A2-63-73-PLC00
                {
                    if (index == 0)//50
                    {

                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band50" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band50.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "50";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,50");
                        }
                    }
                    else if (index == 1)//60
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band60" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band60.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "60";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,60");
                        }
                    }
                    else if (index == 2)//30
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band30" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band30.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "30";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,30");
                        }
                    }
                    else if (index == 3)//52
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band52" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band52.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "52";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,52");
                        }
                    }
                    else if (index == 4)//A2
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0bandA2" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0bandA2.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "A2";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,A2");
                        }
                    }
                    else if (index == 5)//63
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band63" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band63.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "63";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,63");
                        }
                    }
                    else if (index == 6)//73
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band73" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band73.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "73";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,73");
                        }
                    }
                    else if (index == 7)//00
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band00" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band00 PLC.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "00";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,73");
                        }
                    }
                    if (index == 7)//PLC
                    {
                        for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)//R value
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 172)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 172 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                        }
                        for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)//G value
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 178)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 178 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                        }
                        for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)//B value
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 184)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 184 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF))));
                        }
                    }
                    else
                    {
                        for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)      //R value
                        {
                            if (loop < 22)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                            }
                            else if (loop == 22)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 3)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                            }
                            else if (loop >= 23)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 2)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 3)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                            }
                        }
                        for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)  //G value
                        {
                            if (loop < 2)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 56)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 56)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                            }
                            else if (loop == 2)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 57)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                            }
                            else if (loop >= 3 && loop < 11)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                            }
                            else if (loop == 11)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 59)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                            }
                            else if (loop >= 12)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 59)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 59)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                            }

                        }
                        for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)  //B value
                        {
                            if (loop < 24)
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 113)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 113)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF))));
                            }
                            else
                            {
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 114)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF00) >> 8)));
                                sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 114)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF))));
                            }

                        }
                    }
                    sw2.Flush();
                    this.GammaReg_dataGridView.Rows.Clear();
                }
                sw2.Close();
            }
            else if (cbx_IC_index.SelectedIndex == 17)////ICNA3312-DBV����INC3311
            {
                string path_ICN3311 = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\ICN3311_Gamma_readback" + ".txt";
                if (File.Exists(path_ICN3311))
                {
                    File.Delete(path_ICN3311);
                }
                StreamWriter sw2 = File.AppendText(path_ICN3311);

                for (int index = 0; index < 2; index++)//50-60-30-52-A2-63-73-PLC00
                {
                    if (index == 0)//50
                    {
                        sw2.WriteLine("IC WRITE");
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band50" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band50.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "50";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,50");
                        }
                    }
                    else if (index == 1)//60
                    {
                        if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\0.RM690C0band60" + ".csv"))//('0.RM690C0band50.csv'))
                        {
                            MessageBox.Show("0.RM690C0band60.csv not exist!!!");
                            continue;
                        }
                        else
                        {
                            GammaBandSelect.Text = "60";
                            btnSaveGamma_Click(btnLoadGamma, null);
                            sw2.WriteLine("FE,60");
                        }
                    }
                    for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)      //R value
                    {
                        if (loop < 22)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                        }
                        else if (loop == 22)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 3)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                        }
                        else if (loop >= 23)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 2)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 3)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value) & 0xFF))));
                        }
                    }
                    for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)  //G value
                    {
                        if (loop < 2)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 56)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 56)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                        }
                        else if (loop == 2)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 57)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                        }
                        else if (loop >= 3 && loop < 14)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                        }
                        else if (loop == 14)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 58)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 59)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                        }
                        else if (loop >= 15)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 59)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 59)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value) & 0xFF))));
                        }

                    }
                    for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)  //B value
                    {
                        if (loop < 24)
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 113)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 113)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF))));
                        }
                        else
                        {
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 114)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF00) >> 8)));
                            sw2.WriteLine(string.Format("{0:X2}", Convert.ToUInt32(loop * 2 + 1 + 114)) + "," + string.Format("{0:X2}", ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value) & 0xFF))));
                        }

                    }
                    sw2.Flush();
                    this.GammaReg_dataGridView.Rows.Clear();
                }
                sw2.Close();

            }

            else if (cbx_IC_index.SelectedIndex == 0x1F)//WD6300_MIPI_6.7灵动岛 120Hz & 80Hz 2组Gamma
            {
                string filename120Hz = Application.StartupPath + @"\l_gamma\" + cbx_IC_index.SelectedItem.ToString() + "band" + "120" + ".csv";
                string filename80Hz = Application.StartupPath + @"\l_gamma\" + cbx_IC_index.SelectedItem.ToString() + "band" + "80" + ".csv";

                Load_gamma_csv("60");
                string path_6300 = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\gammaband_cfg" + ".wd";
                if (File.Exists(path_6300))
                {
                    File.Delete(path_6300);
                }
                StreamWriter sw = File.AppendText(path_6300);
                //Gamma node
                sw.WriteLine("//Gamma node");
                for (int loop = 1; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0xF0;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0x0F;
                    }
                    addr = 0x5210 + 2 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 4));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow << 4));
                }
                //gamma multiply factor
                sw.WriteLine("//gamma multiply factor");
                for (int loop = 1; loop < 33; loop++)
                {
                    //厂商给的代码是读取寄存器的值，所以cur以及pre需要左移4位
                    int factor = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        if (loop == 1)
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur));
                        }
                        else
                        {
                            int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                            int pre = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop - 1].Cells[1].Value) << 4;
                            factor = (int)Math.Round((double)((1 << 24) - 1) / (cur - pre));//factor计算公式
                        }
                    }

                    addr = 0x5250 + 3 * (loop - 1);
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 16 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor >> 8 & 0xff)));
                    addr++;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", (factor & 0xff)));
                }
                //Gamma node num

                sw.WriteLine("//Gamma value");
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select R");
                sw.WriteLine("wr 0x51f7 0x00");//rif_bc_dbv_gamma_curve_color_sel: select R
                sw.WriteLine("//Red1 AOD");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                Load_gamma_csv("120");
                sw.WriteLine("//Red2 120Hz");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                Load_gamma_csv("60");
                sw.WriteLine("//Red3 60HZ");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Green
                Load_gamma_csv("60");
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select G");
                sw.WriteLine("wr 0x51f7 0x01");
                sw.WriteLine("//Green1  AOD");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                Load_gamma_csv("120");
                sw.WriteLine("//Green2  120Hz");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                Load_gamma_csv("60");
                sw.WriteLine("//Green3  60Hz");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                //Blue
                Load_gamma_csv("60");
                sw.WriteLine("//rif_bc_dbv_gamma_curve_color_sel: select B");
                sw.WriteLine("wr 0x51f7 0x02");//Blue
                sw.WriteLine("//Blue1  AOD");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x8108 + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                Load_gamma_csv("120");
                sw.WriteLine("//Blue2   120Hz");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x814a + 2 * loop;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                Load_gamma_csv("60");
                sw.WriteLine("//Blue3   60Hz");
                for (int loop = 0; loop < 33; loop++)
                {
                    uint nodeHigh = 0x0000;
                    uint nodeLow = 0x0000;
                    if (loop < (this.GammaReg_dataGridView.Rows.Count - 1))
                    {
                        nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                        nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    }
                    addr = 0x818c + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeHigh >> 8));
                    addr = addr + 1;
                    sw.WriteLine("wr 0x" + Convert.ToString(addr, 16) + " 0x" + string.Format("{0:X2}", nodeLow));
                }

                sw.WriteLine("//dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 gamma node num = 0x16");
                if ((this.GammaReg_dataGridView.Rows.Count - 1) < 33)
                {
                    //dcswr 0x7b 0x07 0x07 0x2b 0xf2 0x16 //gamma node num = 0x16
                    sw.WriteLine("wr 0x53b0 0x07");
                    sw.WriteLine("wr 0x53b1 0x07");
                    sw.WriteLine("wr 0x53b2 0x2b");
                    sw.WriteLine("wr 0x53b3 0xf2");
                    sw.WriteLine("wr 0x53b4 0x" + string.Format("{0:X2}", ((this.GammaReg_dataGridView.Rows.Count - 1) & 0xff)));
                }

                sw.Flush();
                sw.Close();
                showmsg("输出gammaband_cfg.wd文件到" + path_6300, 2);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // string path = System.Windows.Forms.Application.StartupPath + "\\l_gamma"  + "edp_gamma.txt";
			if (cbx_IC_index.SelectedIndex == 19)
			{
			    string path ="./edp_gamma.txt";

			    FileStream fs_write = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
			    StreamWriter sw_write = new StreamWriter(fs_write, System.Text.Encoding.UTF8);
			    int[] R_addr = { 0x00, 0x01, 0x06, 0x07, 0x0C, 0x0D, 0x12, 0x13, 0x18, 0x19, 0x1E, 0x1F, 0x24, 0x25, 0x2A, 0x2B, 0x32, 0x33, 0x38, 0x39, 0x40, 0x41, 0x46, 0x47, 0x4C, 0x4D, 0x52, 0x53, 0x59, 0x5A, 0x5F, 0x60, 0x65, 0x66, 0x6B, 0x6C, 0x71, 0x72, 0x77, 0x78, 0x7D, 0x7E, 0x83, 0x84, 0x89, 0x8A, 0x8F, 0x90, 0x95, 0x96 };
			    int[] G_addr = { 0x02, 0x03, 0x08, 0x09, 0x0E, 0x0F, 0x14, 0x15, 0x1A, 0x1B, 0x20, 0x21, 0x26, 0x27, 0x2D, 0x2F, 0x34, 0x35, 0x3A, 0x3B, 0x42, 0x43, 0x48, 0x49, 0x4E, 0x4F, 0x54, 0x55, 0x5B, 0x5C, 0x61, 0x62, 0x67, 0x68, 0x6D, 0x6E, 0x73, 0x74, 0x79, 0x7A, 0x7F, 0x80, 0x85, 0x86, 0x8B, 0x8C, 0x91, 0x92, 0x97, 0x98 };
			    int[] B_addr = { 0x04, 0x05, 0x0A, 0x0B, 0x10, 0x11, 0x16, 0x17, 0x1C, 0x1D, 0x22, 0x23, 0x28, 0x29, 0x30, 0x31, 0x36, 0x37, 0x3D, 0x3F, 0x44, 0x45, 0x4A, 0x4B, 0x50, 0x51, 0x56, 0x58, 0x5D, 0x5E, 0x63, 0x64, 0x69, 0x6A, 0x6F, 0x70, 0x75, 0x76, 0x7B, 0x7C, 0x81, 0x82, 0x87, 0x88, 0x8D, 0x8E, 0x93, 0x94, 0x99, 0x9A };



			    for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
			    {
			        this.GammaReg_dataGridView.Rows[i].Cells[0].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0xFFF) >> 8;
			        this.GammaReg_dataGridView.Rows[i].Cells[1].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[3].Value)) & 0x0FF;
			        this.GammaReg_dataGridView.Rows[i].Cells[2].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0xFFF) >> 8;      
			        this.GammaReg_dataGridView.Rows[i].Cells[3].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[4].Value)) & 0x0FF;
			        this.GammaReg_dataGridView.Rows[i].Cells[4].Value = ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0xFFF) >> 8;
			        this.GammaReg_dataGridView.Rows[i].Cells[5].Value = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[i].Cells[5].Value)) & 0x0FF;

			    }
			    sw_write.WriteLine("IIC_xxbit(03,80,FE,50");
			    for (int loop = 0; loop < 25; loop++)
			    {
			        sw_write.WriteLine("IIC_xxbit(03,80," + (Convert.ToUInt32(R_addr[2 * loop])).ToString("X2") +     "," + ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[0].Value))).ToString("X2"));
			        sw_write.WriteLine("IIC_xxbit(03,80," + (Convert.ToUInt32(R_addr[2 * loop + 1])).ToString("X2") + "," + ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value))).ToString("X2"));
			        sw_write.WriteLine("IIC_xxbit(03,80," + (Convert.ToUInt32(G_addr[2 * loop])).ToString("X2") +     "," + ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[2].Value))).ToString("X2"));
			        sw_write.WriteLine("IIC_xxbit(03,80," + (Convert.ToUInt32(G_addr[2 * loop + 1])).ToString("X2") + "," + ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value))).ToString("X2"));
			        sw_write.WriteLine("IIC_xxbit(03,80," + (Convert.ToUInt32(B_addr[2 * loop])).ToString("X2") +     "," + ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value))).ToString("X2"));
			        sw_write.WriteLine("IIC_xxbit(03,80," + (Convert.ToUInt32(B_addr[2 * loop + 1])).ToString("X2") + "," + ((Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value))).ToString("X2"));

			    }
			    sw_write.Close();
			}
            else if (cbx_IC_index.SelectedIndex == 23 || cbx_IC_index.SelectedIndex == 24)//WD6300_SPI & MIPI
            {
                string path_spi = System.Windows.Forms.Application.StartupPath + "\\l_gamma" + "\\Gamma_SPI" + ".txt";
                if (File.Exists(path_spi))
                {
                    File.Delete(path_spi);
                }
                StreamWriter sw = File.AppendText(path_spi);

                //Gamma node
                sw.WriteLine("//WD6300");
                sw.WriteLine("IC WRITE");
                sw.WriteLine("//Gamma node");
                for (int loop = 1; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    uint nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0xF0;
                    uint nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value)) & 0x0F;
                    //addr = 0x5210 + 2 * (loop - 1);
                    sw.WriteLine("FF,FF,F9,50,52," + string.Format("{0:X2}", Convert.ToUInt32(0x10 + 2 * (loop - 1))) + "," + string.Format("{0:X2}", nodeHigh >> 4) + ",00");
                    sw.WriteLine("FF,FF,F9,50,52," + string.Format("{0:X2}", Convert.ToUInt32(0x10 + 2 * (loop - 1) + 1)) + "," + string.Format("{0:X2}", nodeLow << 4) + ",00");
                }

                //gamma multiply factor
                sw.WriteLine("//gamma multiply factor");
                for (int loop = 1; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    //厂商给的代码是读取寄存器的值，所以cur以及pre需要左移4位
                    int factor;
                    if (loop == 1)
                    {
                        int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                        factor = (int)Math.Round((double)((1 << 24) - 1) / (cur));
                    }
                    else
                    {
                        int cur = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop].Cells[1].Value) << 4;
                        int pre = Convert.ToInt32(this.GammaReg_dataGridView.Rows[loop - 1].Cells[1].Value) << 4;
                        factor = (int)Math.Round((double)((1 << 24) - 1) / (cur - pre));//factor计算公式
                    }
                    //addr = 0x5250 + 3 * (loop - 1);
                    sw.WriteLine("FF,FF,F9,50,52," + string.Format("{0:X2}", Convert.ToUInt32(0x50 + 3 * (loop - 1))) + "," + string.Format("{0:X2}", (factor >> 16 & 0xff)) + ",00");
                    sw.WriteLine("FF,FF,F9,50,52," + string.Format("{0:X2}", Convert.ToUInt32(0x50 + 3 * (loop - 1) + 1)) + "," + string.Format("{0:X2}", (factor >> 8 & 0xff)) + ",00");
                    sw.WriteLine("FF,FF,F9,50,52," + string.Format("{0:X2}", Convert.ToUInt32(0x50 + 3 * (loop - 1) + 2)) + "," + string.Format("{0:X2}", (factor & 0xff)) + ",00");
                }

                //write R curve (//0=wr R   1=wr G   2=wr B)
                //Red
                sw.WriteLine("//Red");
                sw.WriteLine("FF,FF,F9,50,51,f7,00,00");
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    uint nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF00;
                    uint nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[3].Value)) & 0xFF;
                    //addr = 0x8108 + 2 * loop;//RGB地址相同，通过改变0x51f7的值来选择不同的颜色
                    sw.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x08 + 2 * loop)) + "," + string.Format("{0:X2}", nodeHigh >> 8) + ",00");
                    sw.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x08 + 2 * loop + 1)) + "," + string.Format("{0:X2}", nodeLow) + ",00");
                }

                //Green
                sw.WriteLine("//Green");
                sw.WriteLine("FF,FF,F9,50,51,f7,01,00");//Green
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    uint nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF00;
                    uint nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[4].Value)) & 0xFF;
                    sw.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x08 + 2 * loop)) + "," + string.Format("{0:X2}", nodeHigh >> 8) + ",00");
                    sw.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x08 + 2 * loop + 1)) + "," + string.Format("{0:X2}", nodeLow) + ",00");
                }

                //Blue
                sw.WriteLine("//Blue");
                sw.WriteLine("FF,FF,F9,50,51,f7,02,00");//Blue
                for (int loop = 0; loop < (this.GammaReg_dataGridView.Rows.Count - 1); loop++)
                {
                    uint nodeHigh = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF00;
                    uint nodeLow = (Convert.ToUInt32(this.GammaReg_dataGridView.Rows[loop].Cells[5].Value)) & 0xFF;
                    sw.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x08 + 2 * loop)) + "," + string.Format("{0:X2}", nodeHigh >> 8) + ",00");
                    sw.WriteLine("FF,FF,F9,50,81," + string.Format("{0:X2}", Convert.ToUInt32(0x08 + 2 * loop + 1)) + "," + string.Format("{0:X2}", nodeLow) + ",00");
                }
                sw.Flush();
                sw.Close();
            }
        }

        private void btn_Gamma_Log_Save_Click(object sender, EventArgs e)
        {
            if (txttip.Text == string.Empty)
            {
                MessageBox.Show("--空--");
            }
            else
            {
                //鍒涘缓淇濆瓨瀵硅瘽妗嗗璞?
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "Code files (*.txt)|*.txt"; //杩囨护鏂囦欢绫诲瀷
                save.FilterIndex = 1;
                save.RestoreDirectory = true;
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string path = save.FileName;
                    save.DefaultExt = ".txt";
                    StreamWriter sw = new StreamWriter(path);
                    sw.WriteLine(txttip.Text);
                    sw.Close();
                }
            }

        }

        #region Gamma调节
        private void GammaTuning()   //AutoGamma处理过程
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            double scale_ALL=0.85;
			string[] PLC_gamma_r = Gamma_ads_R.Text.Split(',');//
			string[] PLC_gamma_g = Gamma_ads_G.Text.Split(',');
			string[] PLC_gamma_b = Gamma_ads_B.Text.Split(',');
            while (index <= GPN)
            {
                comport_check();//判断串口是否断开
                if (STOPAutoGammaFlag)
                {
                    return;
                }

                txtVT_lum.Text = D_lum[GPN - index].ToString("#0.0000");//当前绑点目标亮度
                numVT_loff.Text = (Diven_Offset[index - 1].LUM_OFF * 100).ToString();//亮度误差百分比
                txtVT_xoff.Text = Diven_Offset[index - 1].X_OFF.ToString();//更新每个绑点的目标亮度、亮度、色度误差
                txtVT_yoff.Text = Diven_Offset[index - 1].Y_OFF.ToString();

				if(Point_RM690C0[GPN - index] > 255)
				{
					if(index ==1)//first node, e.g 295/287
					{
						RM690C0_RegWR_Click(PLC_gamma_r[4]  + "," + PLC_gamma_r[5]);
						RM690C0_RegWR_Click(PLC_gamma_r[6]  + "," + PLC_gamma_r[7]);
						RM690C0_RegWR_Click(PLC_gamma_r[8]  + "," + PLC_gamma_r[9]);
						RM690C0_RegWR_Click(PLC_gamma_r[10] + "," + PLC_gamma_r[11]);
						RM690C0_RegWR_Click(PLC_gamma_r[12] + "," + PLC_gamma_r[13]);
						RM690C0_RegWR_Click(PLC_gamma_r[14] + "," + PLC_gamma_r[15]);
						
					}
					else if(index == 2)//second node, e.g 263/271
					{
						RM690C0_RegWR_Click(PLC_gamma_g[4]  + "," + PLC_gamma_g[5]);
						RM690C0_RegWR_Click(PLC_gamma_g[6]  + "," + PLC_gamma_g[7]);
						RM690C0_RegWR_Click(PLC_gamma_g[8]  + "," + PLC_gamma_g[9]);
						RM690C0_RegWR_Click(PLC_gamma_g[10] + "," + PLC_gamma_g[11]);
						RM690C0_RegWR_Click(PLC_gamma_g[12] + "," + PLC_gamma_g[13]);
						RM690C0_RegWR_Click(PLC_gamma_g[14] + "," + PLC_gamma_g[15]);
					}
					else if(index == 3)//reserve thrid node, for 182 or 132 only 2 node
					{
						RM690C0_RegWR_Click(PLC_gamma_b[4]  + "," + PLC_gamma_b[5]);
						RM690C0_RegWR_Click(PLC_gamma_b[6]  + "," + PLC_gamma_b[7]);
						RM690C0_RegWR_Click(PLC_gamma_b[8]  + "," + PLC_gamma_b[9]);
						RM690C0_RegWR_Click(PLC_gamma_b[10] + "," + PLC_gamma_b[11]);
						RM690C0_RegWR_Click(PLC_gamma_b[12] + "," + PLC_gamma_b[13]);
						RM690C0_RegWR_Click(PLC_gamma_b[14] + "," + PLC_gamma_b[15]);
					}
				}

                if (timeout < 30 && index < 11)//前10个绑点，规格自动尝试收严
                {
                    lum_off = (D_lum[GPN - index] * Convert.ToDouble(numVT_loff.Text)) / 200;  //计算当前绑点允许的亮度误差范围
                    x_off = Convert.ToDouble(txtVT_xoff.Text) / 2 > 0.001 ? (Convert.ToDouble(txtVT_xoff.Text) / 2) : 0.001;
                    y_off = Convert.ToDouble(txtVT_yoff.Text) / 2 > 0.001 ? (Convert.ToDouble(txtVT_yoff.Text) / 2) : 0.001;
                }
                else
                {
                    lum_off = (D_lum[GPN - index] * Convert.ToDouble(numVT_loff.Text)) / 200;  //计算当前绑点允许的亮度误差范围
                    x_off = Convert.ToDouble(txtVT_xoff.Text);
                    y_off = Convert.ToDouble(txtVT_yoff.Text);       //获取色坐标允许误差范围
                }

                ////有时候模组默认亮度超过3000nit，超了410量程，更改W255逻辑为：先写入gamma值，在进行读亮度
                //if (index == 1)
                {
                    //显示当前(待)写入的Gamma寄存器值
                    lab_Rvalue.Text = "R:" + GammaValue[GPN - index, 0].ToString("D4");
                    lab_Gvalue.Text = "G:" + GammaValue[GPN - index, 1].ToString("D4");
                    lab_Bvalue.Text = "B:" + GammaValue[GPN - index, 2].ToString("D4");
                    GammaValueSet((byte)index);
                    label17.Text = timeout.ToString();
                    Thread.Sleep(10);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }
                Thread.Sleep(20);
                Msr_CA310();
                lablvshow.Text = "实时亮度: " + auto_lum.ToString("f4").Substring(0, 6) + "\r\n" + "x: " + auto_x.ToString("f4") + "    y: " + auto_y.ToString("f4") + "\r\n";
                showmsg("(" + auto_x.ToString("F4") + "." + auto_y.ToString("F4") + "." + auto_lum.ToString("F4") + ")", 4);
                showmsg("(" + GammaValue[GPN - index, 0].ToString() + "," + GammaValue[GPN - index, 1].ToString() + "," + GammaValue[GPN - index, 2].ToString() + ")", 4);

                if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))//
                    flag1 = 0;  //判断亮度以及色坐标是否在目标范围
                else flag1 = 1;
                if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                    flag2 = 0;  //判断亮度以及色坐标是否在目标范围
                else flag2 = 1;
                if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                    flag3 = 0;  //判断亮度以及色坐标是否在目标范围
                else flag3 = 1;
                if ((flag1 == 0 && flag2 == 0 && flag3 == 0) && (index < 12)) //低灰阶本来就很难调就不这么折腾了
                {   //初次判断调整OK后，再来复测一次，以消除偶发调不准问题
                    Thread.Sleep(10); Msr_CA310(); Thread.Sleep(10);
                    if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))//
                        flag1 = 0;  //判断亮度以及色坐标是否在目标范围
                    else flag1 = 1;
                    if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                        flag2 = 0;  //判断亮度以及色坐标是否在目标范围
                    else flag2 = 1;
                    if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                        flag3 = 0;  //判断亮度以及色坐标是否在目标范围
                    else flag3 = 1;
                }//初次判断调整OK后，再来复测一次，以消除偶发调不准问题
                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                if (((flag1 == 0 && flag2 == 0 && flag3 == 0) || JumpGammaAdjustFlag) || (timeout > 3 * numTurnGammaTimeout.Value))
                {
                    if (timeout > 3 * numTurnGammaTimeout.Value)
                    {
                        showmsg("....----模糊处理---模糊处理----....", 3);
                    }
                    timeout = 0;/////此处加入提示：判断当前节点亮度是否符合规格上下限。
                    showmsg("Gamma" + GammaBandSelect.Text + ";绑点W" + Point_RM690C0[GPN - index].ToString(), 1);
                    showmsg("目标值(" + D_lum[GPN - index].ToString("F4") + "," + D_x.ToString("F4") + "," + D_y.ToString("F4") + ")", 1); 
                    showmsg("调节值(" + auto_lum.ToString("F4") + "," + auto_x.ToString("F4") + "," + auto_y.ToString("F4") + ")", 1);
                    showmsg("(" + GammaValue[GPN - index, 0].ToString() + ", " + GammaValue[GPN - index, 1].ToString() + ", " + GammaValue[GPN - index, 2].ToString() + ")", 1);
                    if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))
                    { showmsg("====..亮度判定OK..====..", 1); }
                    else 
                    { showmsg("====..亮度判定NG..====..", 1); }
                    showmsg("绑点No" + (GPN - index + 1) + "完成.....", 1);
                    showmsg("绑点W" + Point_RM690C0[GPN - index].ToString() + "调试" + single_timeout.ToString() + "次," + "累计" + Sum_timeout.ToString() + "次。\r\n", 1);
                    this.GammaReg_dataGridView.Rows.Clear();
                    for (byte i = 0; i < GPN; i++)
                    {
                        DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                    }
                    GammaReg_dataGridView.CurrentCell = GammaReg_dataGridView.Rows[Point_RM690C0.Length - index].Cells[0];
                    index++;
                    //showmsg("index is " + index + "-----------...", 1);
                    single_timeout = 0;
                    if (GPN - index < num_index_end.Value)
                    {
                        flag1 = 1;  //复位 亮度色坐标符合目标标志位
                        flag2 = 1;
                        flag3 = 1;
                        if (GPN == 2) index--;//Modify for PLC
                        if (index == GPN)   //  最后一个绑点通常为0，不用调试结束即可
                        {
                            GammaValueSet((byte)index);
                            GammatuningOKFlag = true;
                            showmsg("Gamma" + GammaBandSelect.Text + " 调试完成", 2);  
                            STOPAutoGammaFlag = true;
                            btn_RM690C0_Stop.PerformClick();
                            if(cbx_IC_index.SelectedIndex == 0x1B)////采用系数方式计算0灰阶值
                            {
                                GammaValue[GPN - index, 0] = (uint)(GammaValue[GPN - index + 1, 0] * Convert.ToDouble(num_endlogic[((int)num_index_end.Value - 1 - (GPN - index+1)) * 3 + 0]));
                                GammaValue[GPN - index, 1] = (uint)(GammaValue[GPN - index + 1, 1] * Convert.ToDouble(num_endlogic[((int)num_index_end.Value - 1 - (GPN - index+1)) * 3 + 1]));
                                GammaValue[GPN - index, 2] = (uint)(GammaValue[GPN - index + 1, 2] * Convert.ToDouble(num_endlogic[((int)num_index_end.Value - 1 - (GPN - index+1)) * 3 + 2]));

                            }
                            this.GammaReg_dataGridView.Rows.Clear();
                            for (byte i = 0; i < GPN; i++)
                            {
                                DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                            }

                            if (Multi_gamma_txt_next_index < Multi_gamma_txt_line_count)
                            {
                                Multi_gamma_txt_now_index = Multi_gamma_txt_next_index;
                                Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
                            }
                            return;
                        }
                        GammaValue[GPN - index, 0] = (uint)(GammaValue[GPN - index + 1, 0] * Convert.ToDouble(num_endlogic[((int)num_index_end.Value - 1 - (GPN - index)) * 3 + 0]));
                        GammaValue[GPN - index, 1] = (uint)(GammaValue[GPN - index + 1, 1] * Convert.ToDouble(num_endlogic[((int)num_index_end.Value - 1 - (GPN - index)) * 3 + 1]));
                        GammaValue[GPN - index, 2] = (uint)(GammaValue[GPN - index + 1, 2] * Convert.ToDouble(num_endlogic[((int)num_index_end.Value - 1 - (GPN - index)) * 3 + 2]));


                        lab_Rvalue.Text = "R:" + GammaValue[GPN - index, 0].ToString("D4");
                        lab_Gvalue.Text = "G:" + GammaValue[GPN - index, 1].ToString("D4");
                        lab_Bvalue.Text = "B:" + GammaValue[GPN - index, 2].ToString("D4");
                        GammaValueSet((byte)index);
                        Thread.Sleep(10);
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死        
                        JumpGammaAdjustFlag = true;
                    }
                    else
                    {
                        JumpGammaAdjustFlag = false;
                    }

                    rstep = (byte)(int.Parse(numStep.Value.ToString()));    //重新获取调节步长
                    gstep = (byte)(int.Parse(numStep.Value.ToString()));
                    bstep = (byte)(int.Parse(numStep.Value.ToString()));

                    set_pattern(Point_RM690C0[GPN - index]);
					
					/*
					if(Point_RM690C0[GPN - index] == 263)
					{
						RM690C0_RegWR_Click("FE,40");
						RM690C0_RegWR_Click("D1,08");
						RM690C0_RegWR_Click("FE,73");
					}
					else if(Point_RM690C0[GPN - index] == 271)
					{
						RM690C0_RegWR_Click("FE,40");
						RM690C0_RegWR_Click("D0,78");
						RM690C0_RegWR_Click("FE,73");
					}*/
                    Band_index.Text = "绑点:" + Point_RM690C0[GPN - index].ToString();
                    Thread.Sleep(10);
                }
                else
                {
                    if ((flag3 == 1) | (flag2 == 1) | (flag1 == 1))   //这个判断的意义可以猜猜看
                    {
                        //Gamma --R----
                        if (lumr > D_lums[GPN - index, 0])
                        {
                            if (flag3 == 1)
                            {
                                if ((GammaValue[GPN - index, 0] == 0)&&(index!=0)) { GammaValue[GPN - index, 0] = (uint)(GammaValue[GPN - index + 1, 0] * Convert.ToDouble(scale_ALL)); }
                                if ((!lastdirupR)) { timeout++; }
                                if ((!lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] - rstep;// (byte)(int.Parse(numStep.Value.ToString()));
                                lastdirupR = true;
                            }
                        }
                        else
                        {
                            if (flag3 == 1)
                            {
                                if ((GammaValue[GPN - index, 0] == 0) && (index != 0)) { GammaValue[GPN - index, 0] = (uint)(GammaValue[GPN - index + 1, 0] * Convert.ToDouble(scale_ALL)); }
                                if ((lastdirupR)) { timeout++; }
                                if ((lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] + rstep;
                                lastdirupR = false;
                            }
                        }
                        if ((auto_lum > (D_lum[GPN - index] + lum_off / 2)) || (auto_lum < (D_lum[GPN - index] - lum_off / 2)))//
                        {//Gamma ---G--
                            if (lumg > D_lums[GPN - index, 1])
                            {
                                if ((GammaValue[GPN - index, 1] == 0) && (index != 0)) { GammaValue[GPN - index, 1] = (uint)(GammaValue[GPN - index + 1, 1] * Convert.ToDouble(scale_ALL)); }
                                if ((!lastdirupG)) { timeout++; }
                                if ((!lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] - gstep;
                                lastdirupG = true;
                            }
                            else
                            {
                                if ((GammaValue[GPN - index, 1] == 0) && (index != 0)) { GammaValue[GPN - index, 1] = (uint)(GammaValue[GPN - index + 1, 1] * Convert.ToDouble(scale_ALL)); }
                                if ((lastdirupG)) { timeout++; }
                                if ((lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] + gstep;
                                lastdirupG = false;
                            }
                        }
                        //--Gamma  ---B---
                        if (lumb > D_lums[GPN - index, 2])
                        {
                            if (flag2 == 1)
                            {
                                if ((GammaValue[GPN - index, 2] == 0) && (index != 0)) { GammaValue[GPN - index, 2] = (uint)(GammaValue[GPN - index + 1, 2] * Convert.ToDouble(scale_ALL)); }
                                if ((!lastdirupB)) { timeout++; }
                                if ((!lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] - bstep;
                                lastdirupB = true;
                            }
                        }
                        else
                        {
                            if (flag2 == 1)
                            {
                                if ((GammaValue[GPN - index, 2] == 0) && (index != 0)) { GammaValue[GPN - index, 2] = (uint)(GammaValue[GPN - index + 1, 2] * Convert.ToDouble(scale_ALL)); }
                                if ((lastdirupB)) { timeout++; }
                                if ((lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] + bstep;
                                lastdirupB = false;
                            }
                        }
                    }
                    {//写入Gamma
                        CheckandCorrectGammaValue();
                        //显示当前(待)写入的Gamma寄存器值
                        lab_Rvalue.Text = "R:" + GammaValue[GPN - index, 0].ToString("D4");
                        lab_Gvalue.Text = "G:" + GammaValue[GPN - index, 1].ToString("D4");
                        lab_Bvalue.Text = "B:" + GammaValue[GPN - index, 2].ToString("D4");
                        GammaValueSet((byte)index);
                        label17.Text = timeout.ToString();
                        Thread.Sleep(10);
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死        
                    } //写入Gamma，一共两处都要适配
                }
            }
        }
        private void numStep_ValueChanged(object sender, EventArgs e)   //手动调整AutoGamma调节步长以在初次调整Gamma是提升速度
        {
            rstep = (byte)(int.Parse(numStep.Value.ToString()));
            gstep = (byte)(int.Parse(numStep.Value.ToString()));
            bstep = (byte)(int.Parse(numStep.Value.ToString()));
        }
        #endregion


        #region Gamma调节 UI刷新
        private void CheckandCorrectGammaValue()   //检查GammaValue 是否按照大小排列
        {
            bool Gamma_value_def = false;
            uint GammaValueMax = (uint)num_gammavaluemax.Value - 1;
            byte i; GPN = (byte)Point_RM690C0.Length; // 获取Gamma绑点数量
            GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] & GammaValueMax + 1;
            GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] & GammaValueMax + 1;
            GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] & GammaValueMax + 1;
            if (GammaValue[GPN - 1, 0] > GammaValueMax)
            {
                GammaValue[GPN - 1, 0] = GammaValueMax;
            }
            if (GammaValue[GPN - 1, 1] > GammaValueMax)
            {
                GammaValue[GPN - 1, 1] = GammaValueMax;
            }
            if (GammaValue[GPN - 1, 2] > GammaValueMax)
            {
                GammaValue[GPN - 1, 2] = GammaValueMax;
            }  //确保GammaValue不要超过DDIC 中Gamma寄存能接受的最大值
            if (num_startmode.Value == 1)//适用于昇显data scale
            {
                for (int ab = 0; ab < num_index_start.Value; ab++)
                {
                    GammaValue[GPN - ab - 1, 0] = GammaValue[GPN - (int)num_index_start.Value - 1, 0];
                    GammaValue[GPN - ab - 1, 1] = GammaValue[GPN - (int)num_index_start.Value - 1, 1];
                    GammaValue[GPN - ab - 1, 2] = GammaValue[GPN - (int)num_index_start.Value - 1, 2];
                }
            }
            for (i = (byte)(GPN - 1); i > 0; i--)    //低灰阶GammaValue如果大于高灰阶GammaValue则强制变小
            {
                if (GammaValue[i, 0] < GammaValue[i - 1, 0])
                {
                    GammaValue[i - 1, 0] = GammaValue[i, 0]-1;
                    Gamma_value_def = true;
                }
                if (GammaValue[i, 1] < GammaValue[i - 1, 1] )
                {
                    GammaValue[i - 1, 1] = GammaValue[i, 1] - 1;
                    Gamma_value_def = true;
                }
                if (GammaValue[i, 2] < GammaValue[i - 1, 2])
                {
                    GammaValue[i - 1, 2] = GammaValue[i, 2] - 1;
                    Gamma_value_def = true;
                }
                if (Gamma_value_def == true && gamma_write_mode == true)
                {

                    GammaValueSet((byte)(i + 1));//如果存在低灰阶绑点value值大于高灰阶，那么更新低灰阶值，并写入ic
                }
            }
            //GammaValue[0, 0] = 0; GammaValue[0, 1] = 0; GammaValue[0, 2] = 0;

            if (chkGammaGridViewFresh.Checked)   // 刷新UI界面GammaValue值显示
            {
                this.GammaReg_dataGridView.Rows.Clear();
                for (i = 0; i < GPN; i++)
                {
                    DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                }
                GammaReg_dataGridView.CurrentCell = GammaReg_dataGridView.Rows[Point_RM690C0.Length - index].Cells[0];
            }
        }
        public void DataGirdShow(byte index, double RRR, double GGG, double BBB)        //UI 表格显示 R/G/B对应Gamma寄存器值
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();

            Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.GammaReg_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.GammaReg_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.GammaReg_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.GammaReg_dataGridView.Rows.Add(Row1);
            }
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[0].Value = index + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[1].Value = Point_RM690C0[index].ToString();
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[2].Value = (D_lum[index]).ToString("#0.0000");
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[3].Value = RRR;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[4].Value = GGG;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[5].Value = BBB;

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
                this.GammaReg_dataGridView.Rows[Current_Index].Selected = false;
                this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            GammaReg_dataGridView.FirstDisplayedScrollingRowIndex = GammaReg_dataGridView.RowCount - 1;
        }
        private void btnGammaValueUpdate_Click(object sender, EventArgs e)//刷新UI上表格内的Gamma数据
        {
            int Line_Count = 0;
            try
            {
                Line_Count = GammaReg_dataGridView.RowCount;
            }
            catch
            {
                MessageBox.Show("表格数据(行数)异常！", "提示：");
                return;
            }
            try
            {
                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                {
                    GammaValue[i, 0] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[3].Value);
                    GammaValue[i, 1] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[4].Value);
                    GammaValue[i, 2] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[5].Value);
                }
                //MessageBox.Show("数据更新OK！", "提示：");                
                showmsg("Gamma数据更新OK！", 2);
            }
            catch
            {
                MessageBox.Show("Gamma数据更新错误！", "唐韬说：");
            }
        }
        #endregion


    }
}
